import 'dart:ui';
import 'package:niuniu/common/constant/AppTheme.dart';
import 'package:niuniu/common/constant/Constant.dart';
import 'package:niuniu/common/util/StorageUtil.dart';
import 'package:get/get.dart';
import 'package:package_info_plus/package_info_plus.dart';

/// 配置服务
class ConfigService extends GetxService {
  // 这是一个单例写法
  static ConfigService get to => Get.find();

  PackageInfo? _platform;

  String get version => _platform?.version ?? '-';

  // 多语言
  Locale locale = PlatformDispatcher.instance.locale;

  // Theme 主题
  final RxBool _isDarkModel = Get.isDarkMode.obs;

  bool get isDarkModel => _isDarkModel.value;

  // 是否首次打开
  bool get isFirstOpen => StorageUtil().getBool(Constant.storageFirstOpen);

  @override
  void onReady() {
    super.onReady();
    getPlatform();
    initTheme();
  }

  Future<void> getPlatform() async {
    _platform = await PackageInfo.fromPlatform();
  }

  // 标记已打开app
  void setAlreadyOpen() {
    StorageUtil().setBool(Constant.storageFirstOpen, true);
  }

  // 初始 theme 主题
  void initTheme() {
    var themeCode = StorageUtil().getString(Constant.storageThemeCode);
    _isDarkModel.value = themeCode == "dark" ? true : false;
    Get.changeTheme(
      themeCode == "dark" ? AppTheme.dark : AppTheme.light,
    );
  }

  // 切换 theme 主题
  Future<void> switchThemeModel() async {
    _isDarkModel.value = !_isDarkModel.value;
    Get.changeTheme(
      _isDarkModel.value == true ? AppTheme.dark : AppTheme.light,
    );
    await StorageUtil().setString(Constant.storageThemeCode,
        _isDarkModel.value == true ? "dark" : "light");
  }
}
